﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Functions Exercise 7</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Function that returns the index of the first element in array that is bigger than its neighbors.</h1>
    <label for="arr-input">Array (,):</label>
    <input type="text" id="arr-input" />
    <button onclick="execute()">Execute</button>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function checkIsElementIsBigger(index, arr)
        {
            // Check if index is outside the array
            if ((index < 0) || (index >= arr.length))
            {
                return false;
            }

            // Check lower index (if there is such)
            if ((index - 1 >= 0) && (arr[index - 1] >= arr[index]))
            {
                return false;
            }

            // Check higher index (if there is such)
            if ((index + 1 < arr.length) && (arr[index + 1] >= arr[index]))
            {
                return false;
            }

            return true;
        }

        function findFirstBiggerIndex(arr)
        {
            var firstIdx = -1;

            for (var idx = 0; idx < arr.length; idx++)
            {
                if (false != checkIsElementIsBigger(idx, arr))
                {
                    firstIdx = idx;
                    break;
                }
            }

            return firstIdx;
        }

        function execute()
        {
            var myArrStr = jsConsole.read("#arr-input");
            var myArr = myArrStr.split(",");

            jsConsole.writeLine("First bigger index is " + findFirstBiggerIndex(myArr));
        }

    </script>
</body>
</html>
